package cn.gailvlun.gll.presentation.emotion.util;

import com.haibin.calendarview.Calendar;

import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;

import java.util.Date;
import java.util.Locale;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.presentation.base.App;



public class EmotionDateUtil {
    
    public static String commentTimeFormat(Date time) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime publishTime = new LocalDateTime(time);
        
        long startMillsOfDay = now.toLocalDate().minusDays(1).toDateTimeAtStartOfDay().getMillis();
        
        long endMillsDay = startMillsOfDay + 24 * 60 * 60 * 1000;

        
        long publishMillsOfDay = publishTime.toDate().getTime();

        if (now.getYear() - publishTime.getYear() > 0) {
            return publishTime.toString(App.getInstance().getString(R.string.emotion_time_format_yyyy_mm_dd));
        } else {
            long intervalTime = now.toDate().getTime() - publishTime.toDate().getTime();
            if (startMillsOfDay < publishMillsOfDay && publishMillsOfDay < endMillsDay) {
                return App.getInstance().getString(R.string.emotion_yesterday);
            } else if (intervalTime < 60 * 1000) {
                return App.getInstance().getString(R.string.emotion_now);
            } else if (intervalTime < 60 * 60 * 1000) {
                return App.getInstance().getString(R.string.emotion_few_minute_ago, intervalTime / 60000);
            } else if (intervalTime < 86400000) {
                return App.getInstance().getString(R.string.emotion_few_hour_ago, intervalTime / 3600000);
            } else {
                return publishTime.toString(App.getInstance().getString(R.string.emotion_time_format_mm_dd));
            }

        }
    }

    public static String localeEnglishTimeFormat(Date time) {
        LocalDateTime publishTime = new LocalDateTime(time);
        return publishTime.toString(App.getInstance().getString(R.string.emotion_time_format_mm_yyyy), Locale.ENGLISH);
    }

    public static LocalDateTime dateTimeFormat(Date date) {
        return new LocalDateTime(date);
    }

    public static LocalDate dateFormat(Date date) {
        return new LocalDate(date);
    }

    public static LocalDate dateFormat(int year, int month, int day) {
        String date = year + "/" + month + "/" + day;
        return LocalDate.parse(date, DateTimeFormat.forPattern("yyyy/MM/dd"));
    }

    public static String getCalendarTitle(Calendar calendar) {
        LocalDate localDate = EmotionDateUtil.dateFormat(calendar.getYear(), calendar.getMonth(), calendar.getDay());
        return localDate.toString(App.getInstance().getString(R.string.emotion_time_format_mm_yyyy), Locale.ENGLISH);
    }
}
